package com.lyeducation.controllers.order;

import com.lyeducation.controllers.dto.OrderDTO;
import com.lyeducation.services.order.OrderService;
import com.lyeducation.services.pay.PayService;
import com.lyeducation.util.ResultUtil;
import com.lyeducation.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName OrderController
 * @Description
 * @Author Sky1in3
 * @Date 2019/1/21 17:53
 * @Version 1.0
 */
@RestController
@RequestMapping("/order/{mid}")
@Slf4j
@Transactional(rollbackFor = Exception.class, readOnly = false, propagation = Propagation.REQUIRED)
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private PayService payService;

    /**
     * 查询订单列表
     */
    @GetMapping("/orderList")
    public ResultVO findOrderList(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                  @RequestParam(value = "size", defaultValue = "10") Integer size) {
        return ResultUtil.success(orderService.findList(new PageRequest(page, size)).getContent());
    }

    /**
     * 订单详情
     */
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("orderId") String orderId) {
        return ResultUtil.success(orderService.findOne(orderId));
    }

    /**
     * 取消订单
     */
    @GetMapping("/cancel")
    public ResultVO<OrderDTO> cancel(@RequestParam("orderId") String orderId) {
        return ResultUtil.success(orderService.cancel(orderId));
    }

    /**
     * 完结订单
     */
    @GetMapping("/finish")
    public ResultVO<OrderDTO> finish(@RequestParam("orderId") String orderId) {
        return ResultUtil.success(orderService.finish(orderId));
    }

    /**
     * 支付异步回调
     */
    @PutMapping("/pay/notify")
    public String payNotify(@RequestBody String notify) {
        return payService.notify(notify);
    }

    /**
     * 支付同步回调
     */
    @GetMapping("/pay/return")
    public ResultVO payReturn(@RequestParam String notify) {
        return ResultUtil.success(payService.notify(notify));
    }

    /**
     * 退款审核列表
     */
    @GetMapping("/refundReview")
    public ResultVO refundReview(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                 @RequestParam(value = "size", defaultValue = "10") Integer size) {
        return ResultUtil.success(orderService.refundReview(new PageRequest(page, size)).getContent());
    }

    /**
     * 退款
     */
    @GetMapping("/refundPassed")
    public ResultVO refundPassed(@RequestParam("orderId") String orderId) {
        return ResultUtil.success(orderService.refundPassed(orderId));
    }

    /**
     * 拒绝退款
     */
    @GetMapping("/refundRefuse")
    public ResultVO refundRefuse(@RequestParam("orderId") String orderId) {
        return ResultUtil.success(orderService.refundRefuse(orderId));
    }

}
